package com.longtxt.powerband.util

fun Boolean.toByte() = if (this) 1.toByte() else 0.toByte()

fun Byte.tinyInt() = this.toInt() and 0xFF

private val hexArray = "0123456789ABCDEF".toCharArray()
fun ByteArray.bytesToHex(): String {
    val hexChars = CharArray(this.size * 2)
    this.forEachIndexed { j, byte ->
        val v = (byte.toInt() and 0xFF)
        hexChars[j * 2] = hexArray[v ushr 4]
        hexChars[j * 2 + 1] = hexArray[v and 0x0F]
    }
    val stringBuffer = StringBuilder()
    stringBuffer.append("[ ")
    hexChars.forEachIndexed { index, c ->
        stringBuffer.append(c)
        if (index % 2 == 1) {
            stringBuffer.append(" ")
        }
    }
    stringBuffer.append(" ]")

    return stringBuffer.toString()
}